package com.msmall.base.rms.controller;

import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.msmall.base.PaginationSupport;
import com.msmall.base.PaginationSupportUtil;
import com.msmall.base.ReturnBase;
import com.msmall.base.rms.manager.RMSManager;
import com.msmall.base.rms.po.Group;
import com.msmall.base.rms.util.RMSUtils;
import com.msmall.erp.base.Constant;

@Controller
public class GroupController {

	private static final Logger log = Logger.getLogger(GroupController.class);
	
	@Autowired
	private RMSManager rmsManager;

	
	@RequestMapping("/rms/group/index")
	public String index(HttpServletRequest request, HttpServletResponse response, Map<String, Object> model) {
		try{
			return "rms.group.index";
		}catch(Exception e){
			log.error(e);
			return "forward:" + Constant.SITE_RMS_AUTHERROR + RMSUtils.RMS_ERROR_PARAM + "=" + RMSUtils.RMS_ERROR_SYSTEM;
		}
	}
	
	@RequestMapping("/rms/group/getAuthTree")
	public void getAuthTree(HttpServletRequest request, HttpServletResponse response, @RequestParam Integer groupid) {
		try{
			response.setContentType("text/plain;charset=UTF-8");
			PrintWriter out = response.getWriter();
			JSONObject json = rmsManager.getAuthModuleByAGroupJson(groupid);
			if(json != null){
				out.print(json.getJSONArray("items").toString());
			}else{
				log.info(" =====> NULL ");
				out.print("");
			}
		}catch(Exception e){
			log.error(e);
		}
	}

	@RequestMapping("/rms/group/search")
	public String search(HttpServletRequest request, HttpServletResponse response, @RequestParam String groupName, Map<String, Object> model) {
		try{
			PaginationSupport page = PaginationSupportUtil.get(request);
			model.put("page", page);
			rmsManager.searchGroup(groupName, page);
			return "rms.group.search.result";
		}catch(Exception e){
			log.error(e);
			return "forward:" + Constant.SITE_RMS_AUTHERROR + RMSUtils.RMS_ERROR_PARAM + "=" + RMSUtils.RMS_ERROR_SYSTEM;
		}
	}
	/**
	 * 进入创建/更新实体页面
	 */
	@RequestMapping("/rms/group/entity")
	public String entity(HttpServletRequest request, HttpServletResponse response, @RequestParam Integer groupid, Map<String, Object> model) {
		try{
			if(groupid != null && groupid != 0){
				Group group = rmsManager.getGroup(groupid);
				model.put("entity", group);
			}
			return "rms.group.entity";
		}catch(Exception e){
			log.error(e);
			return "forward:" + Constant.SITE_RMS_AUTHERROR + RMSUtils.RMS_ERROR_PARAM + "=" + RMSUtils.RMS_ERROR_SYSTEM;
		}
	}
	/**
	 * 保存创建/更新的实体
	 */
	@RequestMapping("/rms/group/save")
	public String save(HttpServletRequest request, HttpServletResponse response, Group group, @RequestParam String moduleids) {
		try{
			rmsManager.saveGroup(group, moduleids);
			return "rms.group.index";
		}catch(Exception e){
			log.error(e);
			return "forward:" + Constant.SITE_RMS_AUTHERROR + RMSUtils.RMS_ERROR_PARAM + "=" + RMSUtils.RMS_ERROR_SYSTEM;
		}
	}
	
	/**
	 * 删除模块
	 * @param moduleid
	 * @return
	 */
	@RequestMapping("/rms/group/remove")
	public void remove(HttpServletRequest request, HttpServletResponse response,@RequestParam Integer groupid){
		response.setContentType("text/plain;charset=UTF-8");
		PrintWriter out = null;
		JSONObject json = new JSONObject();
		try{
			out = response.getWriter();
			rmsManager.removeGroup(groupid);
			json.put( ReturnBase.RETURN_CODE_KEY, ReturnBase.RETURN_CODE_SUCCESS);
		}catch(Exception e){
			log.error(e);
			json.put(ReturnBase.RETURN_CODE_KEY, ReturnBase.RETURN_CODE_FAILED);
		}
		out.print(json.toString());
	}
}
